﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ITJZ.BitHelper.APIModel;
//using ITJZ.BitHelper.Tools;
using System.Reflection;
using System.IO;
using System.Xml.Serialization;
using System.Text;

namespace ITJZ.BitHelper.Web.API
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {




            try
            {
                string cla = getMustRequestParameter("class");
                string action = getMustRequestParameter("action");

                if (!string.IsNullOrEmpty(cla) && !string.IsNullOrEmpty(action))
                {

                    //获取到类型
                    Type type = Type.GetType("ITJZ.BitHelper.Web.API." + cla);


                    MethodInfo mf = type.GetMethod(action);

                    ParameterInfo[] pis = mf.GetParameters();
                    object[] obs = new object[pis.Length];
                    for (int i = 0; i < pis.Length; i++)
                    {
                        obs[i] = getMustRequestParameter(pis[i].Name);
                    }

                    object target = type.InvokeMember(null, BindingFlags.CreateInstance, null, null, null);


                    object xmlO = mf.Invoke(target, obs);

                    string html = XmlSerializerHelper.Serialize(xmlO);

                    Response.Write(html);



                }
            }
            catch (Exception ex)
            {
                ResInfo info = new ResInfo();
                info.ErrState = 99;
                info.Msg = ex.InnerException == null ? ex.Message : ex.InnerException.Message;
                string html = XmlSerializerHelper.Serialize(info);
                Response.Write(html);
            }

            Response.End();
        }

        public string getMustRequestParameter(string key)
        {
            string req = Request[key];

            if (string.IsNullOrEmpty(req))
            {
                string msg = string.Format("参数{0}不能为空", key);
                throw new Exception(msg);
            }

            return req;
        }


        ///<summary>
        ///根据类全名在当前程序集里获取类型
        ///</summary>
        ///<paramname="p_TypeFullName">类全名System.Windows.Forms.TextBox</param>
        ///<returns>Type</returns>
        public static Type GetType(string p_TypeFullName)
        {
            if (HttpContext.Current.Application[p_TypeFullName] != null)
            {
                // return HttpContext.Current.Application[p_TypeFullName] as Type;
            }

            Type _TypeInfo = Type.GetType(p_TypeFullName);

            if (_TypeInfo == null)
            {
                Assembly[] _Assembly = AppDomain.CurrentDomain.GetAssemblies();
                for (int i = 0; i != _Assembly.Length; i++)
                {
                    _TypeInfo = _Assembly[i].GetType(p_TypeFullName);

                    if (_TypeInfo != null)
                    {
                        HttpContext.Current.Application[p_TypeFullName] = _TypeInfo;
                        break;
                    }
                }
            }

            if (_TypeInfo == null)
            {
                throw new Exception("未找到类型" + p_TypeFullName);
            }

            return _TypeInfo;
        }



        /// <summary>
        /// 序列化助手类
        /// </summary>
        public class XmlSerializerHelper
        {
            /// <summary>
            /// 把一个对象序列化为xml文档格式
            /// </summary>
            /// <param name="o"></param>
            /// <returns></returns>
            public static string Serialize(object o)
            {
                string res = string.Empty;

                using (MemoryStream ms = new MemoryStream())
                {
                    string temp = o.GetType().FullName;

                    if (true)
                    {
                        
                    }
                    Type tt = HttpContext.Current.Application["typeinfo-" + temp] as Type;

                    if (tt == null)
                    {

                        foreach (Assembly item in AppDomain.CurrentDomain.GetAssemblies())
                        {
                            tt = item.GetType(temp, false, false);
                            if (tt != null)
                            {
                                HttpContext.Current.Application["typeinfo-" + temp] = tt;
                                break;
                            }
                        }
                    }


                    new XmlSerializer(tt).Serialize(ms, o);

                    byte[] bs = ms.ToArray();

                    res = Encoding.UTF8.GetString(bs);
                }

                return res;
            }
        }
    }
}